package com.project.ujob.config;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.project.ujob.properties.DruidConfigProperties;

import lombok.extern.slf4j.Slf4j;

@Configuration
@ImportAutoConfiguration({ DruidConfigProperties.class })
@Slf4j
public class DruidConfig {

	private DruidConfigProperties config;

	DruidConfig(DruidConfigProperties config) {
		this.config = config;
	}

	@Bean
	public DataSource druidDataSource() {
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(config.getUrl());
		datasource.setUsername(config.getUsername());
		datasource.setPassword(config.getPassword());
		datasource.setDriverClassName(config.getDriverClassName());
		datasource.setInitialSize(config.getInitialSize());
		datasource.setMinIdle(config.getMinIdle());
		datasource.setMaxActive(config.getMaxActive());
		datasource.setMaxWait(config.getMaxWait());
		datasource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
		datasource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
		datasource.setValidationQuery(config.getValidationQuery());
		datasource.setTestWhileIdle(config.isTestWhileIdle());
		datasource.setTestOnBorrow(config.isTestOnBorrow());
		datasource.setTestOnReturn(config.isTestOnReturn());
		return datasource;
	}

}
